import time
from multiprocessing import Process

import cv2


class ProcessImageProcess(Process):
    def __init__(self, my_process_queue_image_processing, my_process_queue_image_processed, my_end):
        super(ProcessImageProcess, self).__init__()

        self.my_process_queue_image_processing = my_process_queue_image_processing
        self.my_process_queue_image_processed = my_process_queue_image_processed
        self.my_end = my_end

        self.is_running = True
        self.w = 960
        self.h = 540

    def run(self) -> None:
        time.sleep(0.5)
        while self.is_running:
            flag = self.function()
            if not flag:
                time.sleep(0.0015)

    def stop(self):
        self.is_running = False

    def function(self):
        if self.my_process_queue_image_processing.empty():
            if not self.my_end.empty():
                self.is_running = False
            return False
        else:
            frame, image = self.my_process_queue_image_processing.get()
            image = cv2.resize(image, (self.w, self.h))
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            data = self.video_process()

            self.my_process_queue_image_processed.put([frame, image, data])
            return True

    def video_process(self):
        time.sleep(0.05)
        return []
